package com.vcredit.creditcat.web.api.product;

import com.vcredit.creditcat.util.Utility;
import com.vcredit.creditcat.utils.Base64Utils;
import com.vcredit.creditcat.utils.GZIPUtils;
import com.vcredit.creditcat.utils.RSAUtils;
import com.vcredit.creditcat.vo.payday.CommonRequest;
import com.vcredit.creditcat.vo.payday.CommonResponse;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.JacksonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("api")
public class PayDayProductResponseController {
	
	private static final Logger LOG = LoggerFactory.getLogger(PayDayProductResponseController.class);

	// 信用猫公钥
	@Value("${response.xinyongmao.publicKey}")
	private String publicKey;
	// 合作方私钥
	@Value("${response.parent.privateKey}")
	private String privateKey;

	//准入接口
	@Log
	@RequestMapping("/response")
	@ResponseBody
	public CommonResponse userAccess(HttpServletRequest request, @RequestBody CommonRequest param)
			throws Exception {

		try {
			StringBuffer log=new StringBuffer();

			//1、合作方使用信用猫的RSA公钥进行验签
			String sign =  param.getSign();
			String requsetJson = JacksonUtil.Obj2Json(param);
			System.out.println("网络传输后接受内容: \r\n" + requsetJson);
			log=log.append("网络传输后接受内容：" + requsetJson).append("\n");
			param.setSign(null);
			requsetJson = JacksonUtil.Obj2Json(param);
			Boolean checkResult = RSAUtils.verify(requsetJson.getBytes("UTF-8"), publicKey,sign);

			if (!checkResult) {
				return new CommonResponse().Failure("签名未通过");
			}

			//获取加密后的biz_data数据
			String bizData = param.getBiz_data().toString();
			String base64encodeData=null;

			//2、如果业务数据是压缩过的，那么先进行解压缩
			if (param.getIs_compress() == 1) {
				//对压缩过的业务参数进行解压缩
				byte[] base64compressData = Base64Utils.decode(bizData);
				base64encodeData = GZIPUtils.uncompressToString(base64compressData);

				System.out.println("解压缩后业务字符串： \r\n" + base64encodeData);
				log=log.append("解压缩后业务字符串：" + base64encodeData).append("\n");
				param.setBiz_data(base64encodeData);
			}

			//3、合作方使用自己的RSA私钥进行业务参数的解密
			String bidData = param.getBiz_data().toString();
			byte[] bizDataUnBase64 = Base64Utils.decode(bidData);
			byte[] bizResult = RSAUtils.decryptByPrivateKey(bizDataUnBase64, privateKey);
			param.setBiz_data(bizResult);

			String target = new String(bizResult);
			System.out.println("解密后业务数据内容： \r\n" + target);
			log=log.append("解密后业务数据内容：" + target).append("\n");
			CommonResponse response = new CommonResponse().Success();
			response.setData(new CommonResponse().Success());
			response.setLog(log.toString());
			return response;
		} catch (Exception ex) {
			return new CommonResponse().Failure("系统异常");
		}
	}
}
